package com.ydx.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

//处理页面点击量
@Service
public class PageViewService {

    private static final String PAGE_VIEW_KEY_PREFIX = "page_views:";

    @Autowired
    private RedisTemplate<String, Long> redisTemplate;

    public long getPageViews(String pageId) {
        String key = PAGE_VIEW_KEY_PREFIX + pageId;
        Long views = redisTemplate.opsForValue().get(key);
        return views != null ? views : 0;
    }

    public void incrementPageViews(String pageId) {
        String key = PAGE_VIEW_KEY_PREFIX + pageId;
        redisTemplate.opsForValue().increment(key);
    }
}